<%@ page import="com.deepai.dprsmgr.utils.ErrCode" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@include file="../base_path.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="description" content="">

    <link rel="stylesheet" type="text/css" href="<%=basePath%>/lib/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <link type="text/css" rel="stylesheet" href="<%=basePath%>/lib/bootstrap-select/css/bootstrap-select.min.css"/>
    <link rel="stylesheet" type="text/css" media="screen" href="<%=basePath%>/lib/lobibox/css/lobibox.min.css"/>
    <link rel="stylesheet" type="text/css" media="screen" href="<%=basePath%>/css/mycss.css"/>
    <title>密码修改</title>
</head>
<body>
<div class="navbar-fixed-top">
    <div class="breadcrumb">
        <span>账户管理</span>/
        <span class="active">个人信息</span>
        <span class="pull-right" title="刷新">
            <div class="btn-refresh" onclick="location.replace(location.href);">
            	<img src="<%=basePath%>/images/refresh.png" >
            </div>
        </span>
    </div>
</div>
<div class="container-fluid" style="margin-top: 50px">
	<div class="container-fluid-main">
    <div class="page-header">
        <h3>个人信息</h3>
    </div>
    <div class="container-fluid" style="width: 80%;background: #FFF;">
        <form id="form_modify_pwd" class="form-horizontal" style="padding: 20px;">
            <div class="form-group">
                <label  class="col-md-3 control-label">用户名</label>
                <div class="col-md-6">
                    <p class="form-control-static" id="u_name">${user.username}</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-md-3 control-label">原始密码</label>
                <div class="col-md-6">
                    <button class="btn btn-default btn-file" id="Changepwd">修改密码</button>
                    <input type="password" class="form-control input-sm myhide hide" id="txt_account_passwd" name="txt_account_passwd" placeholder="请输入原密码密码" maxlength="30" autocomplete="off">
                    <span class="help-block myhide hide"></span>
                </div>
            </div>
            <div class="form-group changePwd myhide hide">
                <label for="txt_account_passwd" class="col-md-3 control-label">新密码</label>
                <div class="col-md-6">
                    <input type="password" class="form-control input-sm" id="txt_new_passwd" name="txt_new_passwd" placeholder="请输入新密码" maxlength="30" autocomplete="off">
                    <span class="help-block"></span>
                </div>
            </div>
            <div class="form-group confirmPwd myhide hide">
                <label for="txt_confirm_passwd" class="col-md-3 control-label">确认密码</label>
                <div class="col-md-6">
                    <input type="password" class="form-control input-sm" id="txt_confirm_passwd" name="txt_confirm_passwd" placeholder="请确认密码" maxlength="30" autocomplete="off">
                    <span class="help-block"></span>
                </div>
            </div>
            <div class="form-group myhide hide">
                <label  class="col-md-3 control-label"></label>
                <div class="col-md-6">
                    <button class="btn btn-default btn-file" id="confirmpwd">确认修改</button>
                </div>
            </div>
            <shiro:hasAnyRoles name="dep_leader,quality_specialist,analysis_specialist,call_specialist">
            <div class="form-group">
                <label  class="col-md-3 control-label">所属组织</label>
                <div class="col-md-6">
                    <p class="form-control-static">${user.org.orgName}</p>
                </div>
            </div>
            </shiro:hasAnyRoles>
            <div class="form-group">
                <label  class="col-md-3 control-label">权限角色</label>
                <div class="col-md-6">
                    <p class="form-control-static">${user.role.roleName}</p>
                </div>
            </div>
            <div class="form-group">
                <label  class="col-md-3 control-label">创建时间</label>
                <div class="col-md-6">
                    <p class="form-control-static">${createTime}</p>
                </div>
            </div>
        </form>
    </div>
	</div>
</div>

<script type="text/javascript" src="<%=basePath%>/lib/jquery/1.11.0/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/bootstrap/3.3.7/js/bootstrap.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/bootbox/bootbox.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/jquery.validation/1.14.0/jquery.validate_1.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/lobibox.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/messageboxes.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/lobibox/js/notifications.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/lib/JQuery.md5.js"></script>
<script type="text/javascript" src="<%=basePath%>/js/common.js"></script>

<script type="text/javascript">

    $(function () {
        
        $("#Changepwd").on("click",function (e) {
                $(this).addClass("hide");
                $(".myhide").removeClass("hide");
                $("#form_modify_pwd")[0].reset();
                e.preventDefault();
                e.stopPropagation();
        })


        $("#form_modify_pwd").validate({
            errorPlacement: function(error, element) {
                // Append error within linked label
                //console.log(element)
                $( element )
                    .closest("div.form-group")
                        .find("span.help-block")
                            .append( error );
            },
            errorElement: "p",
            rules: {
                txt_account_passwd: {
                    required: true,
                    isPassword2:true,
                    isRangelen:[6,16]
                },
                txt_new_passwd:{
                    required: true,
                    isPassword2:true,
                    isRangelen:[6,16]
                },
                txt_confirm_passwd: {
                    required: false,
                    equalTo:"#txt_new_passwd"
                }
            },
            messages: {
                txt_account_passwd: {
                    required: "原密码不能为空",
                    isPassword2: "字符或格式有误，请输入6-16个字符，只能包含字母、数字以及标点符号，至少包含2种字符类型",
                    minlength: "至少输入6位字符",
                },
                txt_new_passwd:{
                    required: "密码称不能为空，请输入密码",
                    isPassword2: "字符或格式有误，请输入6-16个字符，只能包含字母、数字以及标点符号，至少包含2种字符类型",
                    minlength: "至少输入6位字符",
                },
                txt_confirm_passwd: {
                    equalTo: "输入密码不一致"
                }
            }
        });

        // 表单提交
        $("#form_modify_pwd").submit(function (event) {
            var username = $("#u_name").text();
            console.log(username)
            if($("#form_modify_pwd").valid()){
                var oldPassword=$.trim($("#txt_account_passwd").val());
                var newPassword=$.trim($("#txt_new_passwd").val());
                if(oldPassword == newPassword){
                    myNotify("error", "", "新旧密码不能相同！");
                    return false;
                }
                // 这里用的是this，如果是Form的话需要Form[0]
                var formData = new FormData(this);
                formData.append("username",username);
                formData.append("oldPassword",oldPassword );
                formData.append("newPassword", newPassword);
                // 阻止当前提交事件，自行实现，否则会跳转
                event.preventDefault();
                $.ajax({
                    url: '<%=basePath%>/account/modifyPwd.do',
                    type: 'POST',
                    data: formData,
                    async: false,
                    // 这两个参数需要被定义，否则报错
                    contentType: false,
                    processData: false,
                    traditional: true,
                    success: function (data) {
                        if (data["errorCode"] == "<%=ErrCode.ERR_CODE_SUCCESS%>") {
                              //  myNotify("success", "", "修改密码成功！");
                            bootbox.alert("密码已修改，请重新登录", function(){ window.location.href = "toMain.do"; });
//                            resetForm();
//                            $("#Changepwd").removeClass("hide");
//                            $(".myhide").addClass("hide");
                        } else {
                                myNotify("error", "", "原密码输入错误！");
                        }
                    },
                    error: function () {
                            myNotify("error", "", "修改过程中发生错误，请联系管理员！");
                    }
                });
            }else{
                    myNotify("error", "", "请更正错误后在进行提交");
            }
            return false;
        });
    });
    /******************************方法声明**********************************/
    // 重置表单
    function resetForm() {
        $("#form_modify_pwd")[0].reset();
    }

</script>

</body>
</html>